/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package actionsAdmin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.beans.Admin;
import models.beans.AdminBean;
import models.dao.AdminDAO;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Group 3 Aprotrain Aptech Group 3 Aprotrain Aptech
 */
public class loginAdmin extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Admin login = (Admin) form;
        AdminBean admin = AdminDAO.login(login.getAdminName(), login.getPassword());
        String userName = request.getParameter("adminName");
        String pass = request.getParameter("password");
        if(userName == null || pass == null){
            request.setAttribute("errorLoginEmpty", "UserName and Password are not correct.");
            return mapping.findForward("loginFalseEmpty");
        }
        if(admin == null){
            request.setAttribute("errorLogin", "UserName and Password are not correct.");
            return mapping.findForward("loginFalse");
        }
        else{
            Admin ad = (Admin) form;
            ad.setAdminName(admin.getAdminName());
            request.getSession().setAttribute("admin", ad);
            return mapping.findForward("loginOk");
        }
    }
}
